import { Body, Controller, Post } from "@nestjs/common";
import { DicomService } from "./dicom.service";
import { WatchFileService } from "../watch-file/watch-file.service";
import { WithGatewayService } from "../micro/with-gateway/with-gateway.service";
import { MessagePattern, RpcException } from "@nestjs/microservices";
import { DicomSystemEvent, SysIntervalMsgType } from "../../../../types/types";

interface reqType {
  scanId: number | string;
  scoutId: any;
  watchFilePath: string;
  totalImgCount: number;
  scanType: string;
}

@Controller("dicom")
export class DicomController {
  constructor(
    private readonly panelService: DicomService,
    private readonly watchFileService: WatchFileService,
    private readonly socketIoGateway: WithGatewayService,
  ) {}

  @MessagePattern(SysIntervalMsgType.DICOM_MSG_EVENT)
  async handleGatewayMsg(msg: any): Promise<any> {
    console.log("收到其他系统发来的消息:", msg);
    const { cmd, data }: { cmd: DicomSystemEvent; data: any } = msg;
    if (cmd === DicomSystemEvent.create_new_fold) {
      try {
        await this.watchFileService.addNewListen(data);
        return "ok";
      } catch (e) {
        throw new RpcException(`rpc error: ${e.message}`);
      }
      // return ;
    }
  }

  mockResponse(scanParams) {
    setTimeout(() => {
      this.socketIoGateway.sendMsg({
        scanId: scanParams[0].scanId,
        scoutId: scanParams[0].scoutId,
        imageIds: {
          studyUID: "1.2.2046.7929.09.1.20230724.105506.19135",
          seriesUID: "1.2.2046.7929.09.1.20230724.105506.07036",
        },
        totalImgCount: 100,
        currentCount: 1,
        currentScanIdFinish: false,
      });
    }, 5000);

    if (scanParams.length === 2) {
      setTimeout(() => {
        this.socketIoGateway.sendMsg({
          scanId: scanParams[1].scanId,
          scoutId: scanParams[1].scoutId,
          imageIds: {
            studyUID: "1.2.826.0.1.3680043.2.461.13044537.2800407881",
            seriesUID:
              "1.2.840.113619.2.472.3.2831161356.403.1654067940.232.6295553",
          },
          totalImgCount: 100,
          currentCount: 1,
          currentScanIdFinish: false,
        });
      }, 5000);
    }
  }
}
